<?php
class img 
{
    public $link = false;
    public $where = false;
    public $_logo = false;
    public $logo_link = 'html/images/base/logo1.png';
    public $_thumb = false;
    public $thumb_size = 70;
    public $x = 500;
    public $y = 425;
    public $pos_x = "RIGHT";
    public $pos_y = "BOTTOM";
    public $quality = 85;
	
	public $w_watermark = 200;
	public $h_watermark = 100;
	
	public $logo_tras = 100;

	

	function upload($post)
    {
	
		if($_FILES[$post]['type'] == 'image/jpeg' || $_FILES[$post]['type'] == 'image/pjpeg')
		{
			if(is_uploaded_file($_FILES[$post]['tmp_name']))
			{
				$file = md5(mt()).'.jpg';
				move_uploaded_file($_FILES[$post]['tmp_name'],'cache/' . $file);
				$the_file = 'cache/'.$file;
			}
		}
		else if($_FILES[$post]['type'] == 'image/png' || $_FILES[$post]['type'] == 'image/x-png')
		{
			if(is_uploaded_file($_FILES[$post]['tmp_name']))
			{
				$file = md5(mt()) . '.png';
				move_uploaded_file($_FILES[$post]['tmp_name'], 'cache/' . $file);
				$the_file = 'cache/'.$file;
			}
		}
        
		if(!$the_file) return "0";
		
        return $the_file;
	}
    
    function create($image){
		$type = strtolower(substr($image, strrpos($image, "."), strlen($image)-strrpos($image, ".")));
		
		if($type==".jpeg" || $type==".jpg")
			return imagecreatefromjpeg($image);
		elseif($type==".png")
			return imagecreatefrompng($image);
		else
			return false;
	}

	function load($the_file,$where)
	{
	
	$this->link = $the_file;
	$this->folder = $this->where.'/'; 
	
	$this->name = mt();
    
	$this->setImage();
    
    $this->big_completo = $this->folder.$this->name.".jpg";
    $this->small_completo = $this->folder."small_".$this->name.".jpg";
    
	unlink($the_file);
	}

	function setImage()
	{
		$this->image = $this->create($this->link);
        $this->w = imagesx($this->image);
        $this->h = imagesy($this->image);


        $this->big_image = $this->resize($this->image,$this->x,$this->y);
        $this->new_w = imagesx($this->big_image);
        $this->new_h = imagesy($this->big_image);


        if($this->thumb) $this->small_image = $this->thumb($this->big_image,$this->thumb_size);

        if($this->_logo && $this->w > $this->w_watermark && $this->h_watermark > 100) $this->logo();


        imagejpeg($this->big_image, $this->folder.$this->name.".jpg",$this->quality);

		if($this->thumb) imagejpeg($this->small_image, $this->folder."small_".$this->name.".jpg",$this->quality);
	}

	function logo()
	{
		$this->logo_img = $this->create($this->logo_link);
		$this->logo_width = imagesx($this->logo_img);
		$this->logo_height = imagesy($this->logo_img);
		
		$this->wt_x = $this->x();
		$this->wt_y = $this->y();
		
		
		imagecopymerge($this->big_image, $this->logo_img, $this->wt_x, $this->wt_y, 0, 0, $this->logo_width, $this->logo_height, $this->logo_tras);
	}

	function x()
	{
		$x = 0;
		switch($this->pos_x)
		{
		case 'CENTER':
            $x = @$this->new_w / 2 - @$this->logo_width / 2;
		break;
		case 'RIGHT':
            $x = @$this->new_w - @$this->logo_width;
		break;
		default:
		$x = 0;
		}
	return $x;
	}
        
	function y()
	{
		$y = 0;
		switch($this->pos_y)
		{
		case 'MIDDLE':
    		$y = @$this->new_h / 2 - @$this->logo_height / 2;
		break;
		case 'BOTTOM':
            $y = @$this->new_h - @$this->logo_height;
		break;
		default:
            $y = 0;
		}
	return $y;
	}
	
	function resize($img, $x_final, $y_final = false)
	{
		$original_x = imagesx($img);
		$original_y = imagesy($img);

		$original_rapporto = $original_x/$original_y;
		
		if ($y_final == false)
		{
			$y_final = $original_x / $original_rapporto;
		}
		
		if (!($original_x > $x_final || $original_y > $y_final))
		{
			$tmp = imagecreatetruecolor($original_x, $original_y);
			imagecopyresampled($tmp, $img, 0, 0, 0, 0, $x_final, $y_final, $x_final, $y_final);
		}
		else
		{
			$temp_original_x = $original_x;
			$temp_original_y = $original_y;
			
			if ( $original_x == $original_y )
			{
				while(true)
				{
					if ( $temp_original_x <= $x_final || $temp_original_y <= $y_final)
						break;	
					
					$temp_original_x--;
					$temp_original_y--;
				}	
			}
			else if ( $original_x > $original_y ) 
			{
				while($temp_original_x > $x_final)
				{
					if ($temp_original_x < 30)
						break;
						
					$temp_original_x--;
					$temp_original_y = $temp_original_x / $original_rapporto;
				}
			}
			else
			{
				while($temp_original_y > $y_final)
				{
					if ($temp_original_y < 30)
						break;
					
					$temp_original_y--;
					$temp_original_x = $temp_original_y * $original_rapporto;
				}
			}
			
			$tmp = imagecreatetruecolor($temp_original_x, $temp_original_y);
			imagecopyresampled($tmp, $img, 0, 0, 0, 0, $temp_original_x, $temp_original_y, $original_x, $original_y);
		}
	return $tmp;
	}
	
    function thumb($myImage,$thumbSize = 70)
    {   
    	$width = imagesx($myImage);
		$height = imagesy($myImage);
    
        if($width > $height) $biggestSide = $width;   
        else $biggestSide = $height;   
        
        $cropPercent = .5;   
        $cropWidth   = $biggestSide*$cropPercent;   
        $cropHeight  = $biggestSide*$cropPercent;   
        
        $c1 = array("x"=>($width-$cropWidth)/2, "y"=>($height-$cropHeight)/2);  
        
        $thumb = imagecreatetruecolor($thumbSize, $thumbSize);   
        imagecopyresampled($thumb, $myImage, 0, 0, $c1['x'], $c1['y'], $thumbSize, $thumbSize, $cropWidth, $cropHeight);
    return $thumb;
    }
}
$img = new img; 
?>